import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


public class MakeRun {

	public static void main(String[] args) throws IOException, TemplateException {
		
		String libPath = args[0];
		URL url = MakeRun.class.getResource("");
		Configuration cfg = new Configuration();
        cfg.setDirectoryForTemplateLoading(new File(url.getFile()));
        Template t = null;
        Writer out =null;
        Map<String, Object> root = new HashMap<String, Object>();
        List<String> jarList = new ArrayList<String>();
        root.put("jarList", jarList);
        
        File file = new File(libPath);
        File[] files = file.listFiles();
        for(File f : files){
        	String fileName = f.getName();
        	if(fileName.endsWith(".jar")){
        		jarList.add(fileName);

        	}
        }
        t = cfg.getTemplate("run_bat.ftl");
        out = new OutputStreamWriter(new FileOutputStream(url.getFile()+"/run.bat"), "UTF-8");
       
        t.process(root, out);
        out.flush();
        out.close(); 
        
        t = cfg.getTemplate("run_sh.ftl");
        out = new OutputStreamWriter(new FileOutputStream(url.getFile()+"/run.sh"), "UTF-8");
        t.process(root, out);
        out.flush();
        out.close();
	}

}
